﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using TierApplication;


namespace OrderMgr.CustomerInfo
{
    public partial class frmCustomerDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _CustomerID = "";
        T_Customer mCustomer = new T_Customer();
        public bool IsPatientSearch = true;
        #endregion

        public frmCustomerDetail()
        {
            InitializeComponent();
        }

        private void frmPatient_Load(object sender, EventArgs e)
        {
            initForm();
            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Enabled = false;

                    showInfo(_CustomerID);
                    break;
                case Utility.OperateMode.Insert:
                    break;
                case Utility.OperateMode.Update:
                    btnSave.Enabled = true;
                    showInfo(_CustomerID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtSex, C_DMB.GetListByType(C_DMB.DType.Sex.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtCustTypeID, C_DMB.GetListByType(C_DMB.DType.CustTypeID.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtCustSource, C_DMB.GetListByType(C_DMB.DType.CustSource.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtRegion, T_Region.GetAllRegionList(), "RegionID", "RegionName");
            Utility.DataToLookupComboxEx(txtCity, T_City.GetCityListByRegion(""), "CityID", "CityName");
        }


        private void showInfo(string sID)
        {
            mCustomer.GetModel(sID);
            //患者左侧基本信息

            this.txtCustID.Text = mCustomer.CustID;
            //this.txtCustNo.Text = mCustomer.CustNo;
            this.txtCustTypeID.EditValue = mCustomer.CustTypeID;
            this.txtSex.EditValue = mCustomer.Sex;
            this.txtCustName.Text = mCustomer.CustName;
            this.txtCustNameEn.Text = mCustomer.CustNameEn;
            this.txtCustSource.EditValue = mCustomer.CustSource;
            this.txtContactName.Text = mCustomer.ContactName;
            //this.txtContactTitle.Text = mCustomer.ContactTitle;
            this.txtContactAddress.Text = mCustomer.ContactAddress;
            //this.txtShipAddress.Text = mCustomer.ContactAddress;
            //this.txtCountry.Text = mCustomer.Country;
            this.txtRegion.EditValue = mCustomer.Region;
            this.txtCity.EditValue = mCustomer.City;
 
            this.txtPostalCode.Text = mCustomer.PostalCode;
            this.txtMobile.Text = mCustomer.Mobile;
            this.txtPhone.Text = mCustomer.Phone;
            this.txtFax.Text = mCustomer.Fax;
            this.txtQQ.Text = mCustomer.QQ;
            this.txtMSN.Text = mCustomer.MSN;
            this.txtEmail.Text = mCustomer.Email;
            //this.txtURL.Text = mCustomer.URL;
            //this.txtBirthday.Text = mCustomer.Birthday.ToString();
            //this.txtPoint.Text = mCustomer.Point.ToString();
            //this.txtCreatDate.Text = mCustomer.CreatDate.ToString();
            //this.txtCreateUser.Text = mCustomer.CreateUser;
            //this.txtIDCardType.Text = mCustomer.IDCardType;
            //this.txtIDCard.Text = mCustomer.IDCard;
            //this.txtUserID.Text = mCustomer.UserID;

        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel ;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mCustomer.CustID = this.txtCustID.Text;
            mCustomer.Sex = (string)this.txtSex.EditValue;
            //mCustomer.CustNo = this.txtCustNo.Text;
            mCustomer.CustTypeID = (string)this.txtCustTypeID.EditValue;
            mCustomer.CustName = this.txtCustName.Text;
            mCustomer.CustNameEn = this.txtCustNameEn.Text;
            mCustomer.CustSource = (string)this.txtCustSource.EditValue;
            mCustomer.ContactName = this.txtContactName.Text;
            //mCustomer.ContactTitle = this.txtContactTitle.Text;
            mCustomer.ContactAddress = this.txtContactAddress.Text;
            //mCustomer.ContactAddress = this.txtShipAddress.Text;
            //mCustomer.Country = this.txtCountry.Text;
            mCustomer.City = (string)this.txtCity.EditValue;
            mCustomer.Region = (string)this.txtRegion.EditValue;
            mCustomer.PostalCode = this.txtPostalCode.Text;
            mCustomer.Mobile = this.txtMobile.Text;
            mCustomer.Phone = this.txtPhone.Text;
            mCustomer.Fax = this.txtFax.Text;
            mCustomer.QQ = this.txtQQ.Text;
            mCustomer.MSN = this.txtMSN.Text;
            mCustomer.Email = this.txtEmail.Text;
            //mCustomer.URL = this.txtURL.Text;
            //mCustomer.Birthday = DateTime.Parse(this.txtBirthday.Text);
            //mCustomer.Point = decimal.Parse(this.txtPoint.Text);
            //mCustomer.CreatDate = DateTime.Parse(this.txtCreatDate.Text);
            //mCustomer.CreateUser = this.txtCreateUser.Text;
            //mCustomer.IDCardType = this.txtIDCardType.Text;
            //mCustomer.IDCard = this.txtIDCard.Text;
            //mCustomer.UserID = this.txtUserID.Text;


            if ((mCustomer.CustTypeID == "") || (mCustomer.CustTypeID == null))
            {
                XtraMessageBox.Show("请选择客户类别");
                return;
            }
            if ((mCustomer.CustName == ""))
            {
                XtraMessageBox.Show("请输入客户名称");
                txtCustName.Focus();
                return;
            }


            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mCustomer.CustID = Utility.GetSeqval(Utility.SeqName.CustID.ToString());
                        mCustomer.Add();
                        _CustomerID = mCustomer.CustID.ToString();
                        txtCustID.Text = _CustomerID;
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("客户信息新增成功!");
                    DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        mCustomer.CustID = txtCustID.Text;
                        mCustomer.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("客户信息修改出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("客户信息修改成功!");
                    DialogResult = DialogResult.OK;
                    break;
                default:
                    break;
            }
            //FormCloseReturnValue();
            DialogResult = System.Windows.Forms.DialogResult.OK;
            //Close();
        }

        private void txtCustName_EditValueChanged(object sender, EventArgs e)
        {
            if ((string)txtCustTypeID.EditValue == "0")
                txtContactName.Text = txtCustName.Text;
        }

        private void txtRegion_EditValueChanged(object sender, EventArgs e)
        {
            Utility.DataToLookupComboxEx(txtCity, T_City.GetCityListByRegion((string)txtRegion.EditValue), "CityID", "CityName");
        }
    }
}